<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/7/10
 * Time: 15:33
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsSpecRelation extends ActiveRecord{
    public static function tableName(){
        return '{{%goods_spec_relation}}';
    }

    //获取商品颜色规格
    public function getColorSpecByMainId($main_goods_id){
        $spec_color_list  = array();
        $where = ['r.use_flg'=>1,'r.main_goods_id'=>$main_goods_id,'r.spec_type'=>1];

        $result = $this::find()->where($where)->alias('r')->orderBy('r.sort asc')->select('gs.goods_id,r.spec_id,r.main_goods_id,s.img_url')->
            leftJoin('j_goods_spec as s','r.spec_id = s.id')->
            leftJoin('j_goods_sale as gs','r.main_goods_id = gs.main_goods_id AND r.spec_id = gs.spec_color')->asArray()->all();
        if(!empty($result)){
            foreach($result as $value){
                $spec_color_list[$value['main_goods_id']][$value['spec_id']] = $value;
            }
        }else{
            $spec_color_list = array();
        }

        return $spec_color_list;
    }

    //获取商品规格
    public function getSpecList($main_goods_id,$format_id,$color_id){
        $spec_list  = array();
        $spec_format = array();//规格
        $spec_color = array();//颜色
        $where = ['and',['r.use_flg'=>1],['r.main_goods_id'=>$main_goods_id],['or',['r.spec_id'=>$format_id],['r.spec_id'=>$color_id]]];
        //or 的条件用来过滤没有sku的规格
        $result = $this::find()->where($where)->alias('r')->orderBy('r.sort asc')->select('r.spec_id,r.main_goods_id,r.color_pic as img_url,s.spec_val,r.spec_type,r.remark')->
        leftJoin('j_goods_spec as s','r.spec_id = s.id')->asArray()->all();

        if(!empty($result)){
            $list = \common\helps\tools::uniqueArray($result,'spec_id');
            foreach($list as $value){
                if(in_array($value['spec_id'],$format_id)){
                    $spec_format[] = $value;
                }
                if(in_array($value['spec_id'],$color_id)){
                    $spec_color[] = $value;
                }
            }
        }
        $spec_list['spec_format'] = empty($spec_format) ? array() : $spec_format;
        $spec_list['spec_color'] = empty($spec_color) ? array() : $spec_color;
        return $spec_list;
    }

    //获取商品颜色规格信息
    public function getSpecListColor($main_goods_id,$color_id){
        $where = ['and',['r.use_flg'=>1],['r.main_goods_id'=>$main_goods_id],['r.spec_id'=>$color_id]];
        //or 的条件用来过滤没有sku的规格
        $result = $this::find()->where($where)->alias('r')->orderBy('r.sort asc')->select('r.spec_id,r.main_goods_id,r.color_pic as img_url,s.spec_val,r.spec_type,r.remark')->
        leftJoin('j_goods_spec as s','r.spec_id = s.id')->asArray()->all();
        if(!empty($result)){
            $list = \common\helps\tools::uniqueArray($result,'spec_id');
        }else{
            $list = array();
        }
        return $list;
    }

    /**
     * 获取商品规格列表
     * @param $format_id array  规格组
     * @param $color_id array  颜色组
     */
    public function getSpecListOther($format_id,$color_id){
        $spec_id = array_merge($format_id,$color_id);
    }

    public function getMain($main_goods_id){
        $result = array();
        $where = ['and',['gs.main_goods_id'=>$main_goods_id],['gs.use_flg'=>1],['!=','gs.spec_color',0]];
        $list = GoodsSale::find()->where($where)->alias('gs')->select('gs.main_goods_id,gs.goods_id,gs.spec_color')->asArray()->all();
        if(!empty($list)){
            foreach($list as $value){
                $result[$value['main_goods_id']][] = $value;
            }
        }
        return $result;
    }
}